﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using RapidSalesAdminPortalAPI.DataAccess;
using RapidSalesAdminPortalAPI.Models;
using System.Collections.Generic;
using System.Data;
using WebAPI.Common;
using WebAPI.Util;
using static WebAPI.Models.Enums;

namespace RapidSalesAdminPortalAPI.Controllers
{
    /// <summary>
    /// H5-Master Data
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MasterDataFrontController : ParentFrontController
    {
        private readonly RS_AdmPortalContext _context;
        private readonly IConfiguration _configuration;
        private readonly ILogUtility _logUtility;
        private readonly ILogger _logger;
        private readonly IDataProtectorUtility _dataProtectorUntility;

        public MasterDataFrontController(RS_AdmPortalContext context, IConfiguration configuration, ILogUtility logUtility, ILogger<ProductController> logger, IDataProtectorUtility dataProtectorUntility)
        {
            _context = context;
            _configuration = configuration;
            _logUtility = logUtility; // sample code for DI of common utility class
            _logger = logger;
            _dataProtectorUntility = dataProtectorUntility;
        }
                
        [HttpPost("GetMasterDataList")]
        public async Task<ActionResult<ResultMsg>> GetMasterDataList([FromBody] GetMasterDataListRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                string optionSetGroup = "4"; //Master Data for H5
                IQueryable<FrIpMasterDatum> paComont = _context.FrIpMasterData.AsQueryable();
                paComont = paComont.Where(x => x.Enable == true);
                paComont = paComont.Where(x => x.OptionSetGroup == optionSetGroup);
                var paData = paComont.ToList().Select(x => new GetMasterDataListResponse()
                {
                    Code = x.Code,
                    Name = x.Name,
                    Value = x.Value
                }).AsQueryable();

                return resultMsg.SetResultMsg(
                     (int)ResultCode.Success,
                     HelperUtility.GetDescription(ResultCode.Success),
                     paData.ToList());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                     (int)ResultCode.ErrorOnRequestUnMarshalling,
                     HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                     ex.Message);
            }
        }

        [HttpPost("GetLangDataList")]
        public async Task<ActionResult<ResultMsg>> GetLangDataList([FromBody] GetLangDataListRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                IQueryable<FrIpMultiLangSet> paComont = _context.FrIpMultiLangSets.AsQueryable();
                paComont = paComont.Where(x => x.Enable == true);
                paComont = paComont.Where(x => x.LangType == requestModel.lang);
                var paData = paComont.ToList().Select(x => new GetLangDataListResponse()
                {
                    Code = x.Code,
                    Value = x.Value
                }).AsQueryable();

                return resultMsg.SetResultMsg(
                     (int)ResultCode.Success,
                     HelperUtility.GetDescription(ResultCode.Success),
                     paData.ToList());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                     (int)ResultCode.ErrorOnRequestUnMarshalling,
                     HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                     ex.Message);
            }
        }

    }
}
